package compile

import (
	"path"
	"path/filepath"
	"strings"

	"sail/utils/github.com/go-ffmt/ffmt"
)

func ident(depth int) string {
	return strings.Join(make([]string, depth), "    ")
}

func parseFileName(filePath string) string {
	base := path.Base(filePath)
	ext := path.Ext(filePath)

	return strings.TrimSuffix(base, ext)
}

func dotPathToString(dotPath *DotPath) string {
	var dpPartStrItems []string

	for _, id := range dotPath.Parts {
		dpPartStrItems = append(dpPartStrItems, id.Value)
	}

	return strings.Join(dpPartStrItems, ".")
}

func dotPathToFilePath(dotPath *DotPath) (path string) {
	if len(dotPath.Parts) == 0 {
		return "."
	}

	var parts []string

	for _, id := range dotPath.Parts {
		parts = append(parts, id.Value)
	}

	return strings.Join(parts, string(filepath.Separator))
}

func dotPathToStringSlice(dotPath *DotPath) (parts []string) {
	for _, part := range dotPath.Parts {
		parts = append(parts, part.Value)
	}
	return
}

func stringSliceEquals(a []string, b []string) bool {
	if len(a) != len(b) {
		return false
	}

	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}

	return true
}

func stringSliceIn(strSlice []string, strSArray [][]string) bool {
	for _, s := range strSArray {
		if stringSliceEquals(s, strSlice) {
			return true
		}
	}
	return false
}

func stringIn(str string, strSlice []string) bool {
	for _, s := range strSlice {
		if s == str {
			return true
		}
	}
	return false
}

func printStruct(i interface{}) {
	ffmt.P(i)
}
